package cn.iocoder.yudao.module.peizhen.service.withdraw;

import cn.hutool.core.util.IdUtil;
import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
import cn.iocoder.yudao.module.peizhen.controller.app.withdraw.vo.AppWithdrawPageReqVO;
import cn.iocoder.yudao.module.peizhen.controller.app.withdraw.vo.AppWithdrawSaveReqVO;
import cn.iocoder.yudao.module.peizhen.dal.dataobject.opencity.ProgramDO;
import cn.iocoder.yudao.module.peizhen.dal.dataobject.staff.StaffDO;
import cn.iocoder.yudao.module.peizhen.dal.dataobject.withdraw.WithDrawAndStaffDO;
import cn.iocoder.yudao.module.peizhen.dal.mysql.staff.StaffMapper;
import cn.iocoder.yudao.untils.OrderNumUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.peizhen.controller.admin.withdraw.vo.*;
import cn.iocoder.yudao.module.peizhen.dal.dataobject.withdraw.WithdrawDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.peizhen.dal.mysql.withdraw.WithdrawMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.peizhen.enums.ErrorCodeConstants.*;

/**
 * 员工提现 Service 实现类
 *
 * @author duanxinli
 */
@Service
@Validated
public class WithdrawServiceImpl implements WithdrawService {

    @Resource
    private WithdrawMapper withdrawMapper;
    @Resource
    private StaffMapper staffMapper;

    @Override
    public Integer createWithdraw(WithdrawSaveReqVO createReqVO) {
        // 插入
        WithdrawDO withdraw = BeanUtils.toBean(createReqVO, WithdrawDO.class);
        withdrawMapper.insert(withdraw);
        // 返回
        return withdraw.getId();
    }

    @Override
    public void updateWithdraw(WithdrawSaveReqVO updateReqVO) {
        // 校验存在
        validateWithdrawExists(updateReqVO.getId());
        // 更新
        WithdrawDO updateObj = BeanUtils.toBean(updateReqVO, WithdrawDO.class);
        withdrawMapper.updateById(updateObj);
    }

    @Override
    public void deleteWithdraw(Integer id) {
        // 校验存在
        validateWithdrawExists(id);
        // 删除
        withdrawMapper.deleteById(id);
    }

    private void validateWithdrawExists(Integer id) {
        if (withdrawMapper.selectById(id) == null) {
            throw exception(WITHDRAW_NOT_EXISTS);
        }
    }

    @Override
    public WithdrawDO getWithdraw(Integer id) {
        return withdrawMapper.selectById(id);
    }

    @Override
    public PageResult<WithDrawAndStaffDO> getWithdrawPage(WithdrawPageReqVO pageReqVO) {
        return withdrawMapper.selectPage(pageReqVO);
    }


    //app
    @Override
    @Transactional
    public Integer createWithdraw(AppWithdrawSaveReqVO createReqVO) {
        //检验是否有未审核申请
        WithdrawDO withdrawDO = withdrawMapper.selectOne(WithdrawDO::getStaffId, createReqVO.getStaffId(), WithdrawDO::getStatus, 0);
        if (withdrawDO!=null){
            throw exception(WITHDRAW_NOT_JIESUAN_EXISTS);
        }
        // 插入
        WithdrawDO withdraw = BeanUtils.toBean(createReqVO, WithdrawDO.class);
        withdraw.setOrderNum(OrderNumUtil.generateInvoiceNumber());
        withdrawMapper.insert(withdraw);
        //对账户进行结算
        StaffDO staff = staffMapper.selectById(createReqVO.getStaffId());
        if (staff.getBalance()>=createReqVO.getMoney()){
            staff.setBalance(staff.getBalance()-createReqVO.getMoney());
            staffMapper.updateById(staff);
        }
        // 返回
        return withdraw.getId();
    }

    @Override
    public void updateWithdraw(AppWithdrawSaveReqVO updateReqVO) {
        // 校验存在
        validateWithdrawExists(updateReqVO.getId());
        // 更新
        WithdrawDO updateObj = BeanUtils.toBean(updateReqVO, WithdrawDO.class);
        withdrawMapper.updateById(updateObj);
    }




    @Override
    public PageResult<WithDrawAndStaffDO> getWithdrawPage(AppWithdrawPageReqVO pageReqVO) {

        return withdrawMapper.selectPage(pageReqVO);
    }

    @Override
    public void checkWithdraw(WithdrawSaveReqVO updateReqVO) {

        WithdrawDO withdrawDO = withdrawMapper.selectById(updateReqVO.getId());

        withdrawDO.setStatus(updateReqVO.getStatus());
        // 更新
        withdrawMapper.updateById(withdrawDO);
    }
}